﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using MasoQna.Models;
using MasoQna.Infrastructure;
using CsXssGuard;

namespace MasoQna.Controllers
{
    public class QuestionsController : Controller
    {
        private MqDbContext context = new MqDbContext();

        public ActionResult Read(int id)
        {
            Question question =
                (from q in context.Writings.OfType<Question>()
                    .Include(q => q.Writer)
                 where q.Id == id
                 select q).SingleOrDefault();

            question.Answers = (from a in context.Writings.OfType<Answer>()
                                    .Include(a => a.Writer)
                                where a.QuestionId == id
                                select a).ToList();

            return View(question);
        }

        public ActionResult Write()
        {
            return View(new Question());
        }

        public ActionResult Remove(Question q){
            Question question = (from qu in context.Writings.OfType<Question>()
                                    where qu.Id == q.Id
                                    select qu).SingleOrDefault();
            context.Writings.Remove(question);
            context.SaveChanges();

            return Redirect("/");
        }

        [HttpPost]
        public ActionResult Write(Question q)
        {
            int userId = int.Parse(User.Identity.Name);
            User writer = (from u in context.Users
                           where u.Id == userId
                           select u).SingleOrDefault();
            q.Time = DateTime.Now;
            q.Writer = writer;
            context.Writings.Add(q);
            context.SaveChanges();
            return Redirect("/");
            //return RedirectToAction("Read", new { id = q.Id });
        }

        [HttpPost]
        public ActionResult Edit(Question q){
            Question question = (from qu in context.Writings.OfType<Question>()
                                    where qu.Id == q.Id
                                    select qu).SingleOrDefault();
            question.Title = q.Title;
            question.Content = q.Content;

            context.SaveChanges();

            return RedirectToAction("Read", new { id = q.Id });
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult PostAnswer(int questionId, string answerText)
        {
            if (Request.IsAuthenticated == false)
            {
                return View("PostAnswer_Error_NoAuth");
            }

            Answer answer = new Answer();
            int writerId = Int32.Parse(User.Identity.Name);
            User user = (from u in context.Users
                         where u.Id == writerId
                         select u).SingleOrDefault();

            HtmlSanitizer s = new HtmlSanitizer();
            answer.Content = s.Sanitize(answerText);
            answer.Writer = user;
            answer.Time = DateTime.Now;
            answer.QuestionId = questionId;

            context.Writings.Add(answer);
            context.SaveChanges();

            return PartialView("ReadPart", answer);
        }
    }
}
